package cn.linkea.boot.core.service.impl;

import javax.annotation.Resource;
import java.util.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.springframework.stereotype.Service;
import org.springframework.beans.BeanUtils;

import com.github.wxiaoqi.security.checkout.common.utils.convert.GenericConvert;
import com.github.wxiaoqi.security.checkout.common.utils.lang.Paginator;
import com.github.wxiaoqi.security.checkout.common.utils.util.PageList;
import com.github.wxiaoqi.security.checkout.common.utils.CommonUtils;
import com.github.wxiaoqi.security.checkout.common.dal.daointerface.CompanyLoginDAO;
import com.github.wxiaoqi.security.checkout.common.dal.dataobject.CompanyLoginDO;
import com.github.wxiaoqi.security.checkout.model.CompanyLoginModel;
import com.github.wxiaoqi.security.checkout.service.CompanyLoginService;

/**
* 类名: CompanyLoginServiceImpl
* @author wjt
*/
@Service("companyLoginService")
public class CompanyLoginServiceImpl implements CompanyLoginService {

    @Resource
    private CompanyLoginDAO companyLoginDAO;
	
    @Override
    public CompanyLoginModel findById(Long id) {
        CompanyLoginDO companyLoginDO = companyLoginDAO.findById(id);
        CompanyLoginModel companyLoginModel = new CompanyLoginModel();
        if(companyLoginDO != null) BeanUtils.copyProperties(companyLoginDO, companyLoginModel);
        return companyLoginModel;
    }

    @Override
    public Map save(CompanyLoginModel companyLoginModel) {
        // 处理结果
        Map<String, Object> result = new HashMap<>();
        result.put("success", true);

        // 参数转换
        CompanyLoginDO companyLoginDO = new CompanyLoginDO();
        BeanUtils.copyProperties(companyLoginModel, companyLoginDO);

        int i;
        // 修改
        if (companyLoginDO.getCompanyLoginId() != null && companyLoginDO.getCompanyLoginId() != 0) {
            companyLoginDO.setModifyTime(new Date());
            i = companyLoginDAO.update(companyLoginDO);
            result.put("data", i);
            return result;
        }

        // 设置默认参数
        companyLoginDO.setIsFlag("0");      // 状态：0-有效
        companyLoginDO.setDelFlag("0");     // 是否删除: 0-有效,1-删除
        companyLoginDO.setCreateTime(new Date());

        // 新增
        i = companyLoginDAO.add(companyLoginDO);
        result.put("data", i);
        return result;
    }

    @Override
    public int delete(Long id) {
        CompanyLoginDO companyLoginDO = new CompanyLoginDO();
        companyLoginDO.setCompanyLoginId(id);
        companyLoginDO.setDelFlag("1");
        companyLoginDO.setDelTime(new Date());
        return companyLoginDAO.update(companyLoginDO);
    }

    @Override
    public List<CompanyLoginModel> listByParams(CompanyLoginModel companyLoginModel) {
        Map<String,Object> params = new HashMap<String,Object>();

        // 参数转换
        CompanyLoginDO companyLoginDO = new CompanyLoginDO();
        BeanUtils.copyProperties(companyLoginModel, companyLoginDO);
        params = CommonUtils.getStringObjectMap(companyLoginDO, params);

        List<CompanyLoginDO> companyLoginDOList = companyLoginDAO.findListByParams(params);
        List<CompanyLoginModel> companyLoginList = null;
        if(companyLoginDOList != null && companyLoginDOList.size() > 0){
            companyLoginList = GenericConvert.convert(companyLoginDOList, CompanyLoginModel.class);
        }
        return companyLoginList;
    }

    @Override
    public PageList<CompanyLoginModel> pageListByParams(CompanyLoginModel companyLoginModel, int pageIndex, int pageSize) {

        // 参数转换
        CompanyLoginDO companyLoginDO = new CompanyLoginDO();
        BeanUtils.copyProperties(companyLoginModel, companyLoginDO);
        Map<String,Object> params = new HashMap<String,Object>();
        params = CommonUtils.getStringObjectMap(companyLoginDO, params);

        PageList<CompanyLoginModel> pageList = new PageList<CompanyLoginModel>();
        int count = companyLoginDAO.count(params);
        if (count > 0) {
            Paginator paginator = new Paginator(pageSize, count);
            paginator.setPage(pageIndex);
            params.put("beginIndex", paginator.getOffset());
            params.put("pageSize", pageSize);
            pageList.setPaginator(paginator);

            // List DO 转 Model
            List<CompanyLoginDO> list = companyLoginDAO.findPageListByParams(params);
            List<CompanyLoginModel> models = new ArrayList<CompanyLoginModel>();
            for (CompanyLoginDO obj : list) {
               CompanyLoginModel model = new CompanyLoginModel();
               BeanUtils.copyProperties(obj, model);
               models.add(model);
            }
            pageList.addAll(models);
        }

        return pageList;
   }


}
